package com.inspur.edp.formserver.vmmanager.increment;

import com.fasterxml.jackson.core.JsonProcessingException;
import com.fasterxml.jackson.databind.ObjectMapper;
import com.fasterxml.jackson.databind.module.SimpleModule;
import com.inspur.edp.das.commonmodel.entity.increment.CommonModelIncrement;
import com.inspur.edp.formserver.viewmodel.increment.ViewModelIncrement;
import com.inspur.edp.formserver.viewmodel.json.increment.ViewModelIncrementDeserializer;
import com.inspur.edp.formserver.viewmodel.json.increment.ViewModelIncrementSerializer;
import com.inspur.edp.metadata.rtcustomization.api.ICustomizedContent;
import com.inspur.edp.metadata.rtcustomization.spi.CustomizationSerializer;

public class ViewModelCustomizationSerializer implements CustomizationSerializer {

    private ObjectMapper mapper;
    private ObjectMapper getMapper(){
        if(mapper == null){
            mapper = new ObjectMapper();
//            SimpleModule module = new SimpleModule();
//            module.addSerializer(ViewModelIncrement.class, new ViewModelIncrementSerializer());
//            module.addDeserializer(ViewModelIncrement.class, new ViewModelIncrementDeserializer());
//            mapper.registerModule(module);
        }
        return mapper;
    }

    @Override
    public String serialize(ICustomizedContent increament) {
        try {
            return getMapper().writeValueAsString(increament);
        } catch (JsonProcessingException e) {
            throw new RuntimeException("vo元数据增量序列化失败",e);
        }
    }

    @Override
    public ICustomizedContent deSerialize(String increamentStr) {
        try {
            return getMapper().readValue(increamentStr,ViewModelIncrement.class);
        } catch (JsonProcessingException e) {
            throw new RuntimeException("vo元数据增量反序列化失败",e);
        }
    }
}
